package cn.fl.ref.fncprjpaysch.service.impl;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.Pager ;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncprjpaysch.service.IFncPrjPaySchService;
import cn.fl.ref.fncprjpaysch.transform.FncPrjPayRegVOTransform;
import cn.fl.ref.fncprjpaysch.transform.FncPrjPaySchDJrnlVOTransform;
import cn.fl.ref.fncprjpaysch.transform.FncPrjPaySchDVOTransform;
import cn.fl.ref.fncprjpaysch.transform.FncPrjPaySchMJrnlVOTransform;
import cn.fl.ref.fncprjpaysch.transform.FncPrjPaySchMVOTransform;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPayRegVO;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPaySchDJrnlVO;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPaySchDVO;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPaySchMJrnlVO;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPaySchMVO;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysUsrVO;
import cn.fl.system.service.ISysBkAccInfoService;
import cn.fl.system.vo.SysBkAccInfoVO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.ref.contract.condition.FncPrjContMainQueryCondition;
import fl.ref.contract.constant.FncPrjContMainConstant;
import fl.ref.fncprjpaysch.condition.FncPrjPaySchMCondition;
import fl.ref.fncprjpaysch.dto.FncPrjPayRegDTO;
import fl.ref.fncprjpaysch.dto.FncPrjPaySchDDTO;
import fl.ref.fncprjpaysch.dto.FncPrjPaySchDJrnlDTO;
import fl.ref.fncprjpaysch.dto.FncPrjPaySchMDTO;
import fl.ref.fncprjpaysch.dto.FncPrjPaySchMJrnlDTO;
import fl.ref.fncprjpaysch.service.IFncPrjPaySchApiService;
import fl.ref.fribaseinfo.dto.BankAccInfoDTO;
import fl.ref.fribaseinfo.service.IBankAccInfoApiService;

@Service
public class FncPrjPaySchServiceImpl implements IFncPrjPaySchService {
	private static final Logger logger = LoggerFactory.getLogger(FncPrjPaySchServiceImpl.class);
	@Autowired
	private IFncPrjPaySchApiService fncPrjPaySchApiService;
	/**
	 *ISysUsrService 服务层BEAN
	 */
	@Autowired
	private ISysUsrService sysUsrService;
	@Autowired
	private ISysBkAccInfoService sysBkAccInfoService;

	@Autowired
	private IBankAccInfoApiService bankAccInfoApiService;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService; //数据字典
	
	
	@Override
	public List<Map<String, Object>> selectRepayList(Long fundRasInsId) throws BaseException {
		return fncPrjPaySchApiService.selectRepayList(fundRasInsId);
	}

	@Override
	public Map<String, Object> selectRepayProjInfo(long id) throws BaseException {
		return fncPrjPaySchApiService.selectRepayProjInfo(id);
	}

	@Override
	public List<FncPrjPaySchDVO> selectRepayDetlLst(long id) throws BaseException {
		List<FncPrjPaySchDDTO> dtoLst = fncPrjPaySchApiService.selectRepayDetlLst(id);
		List<FncPrjPaySchDVO> voLst = FncPrjPaySchDVOTransform.toVOList(dtoLst);
		if (voLst!=null) {
			for (FncPrjPaySchDVO vo:voLst) {
				if (vo.getPayAccId()!=null) {
					SysBkAccInfoVO bkAccInfoVO = sysBkAccInfoService.selectById(vo.getPayAccId());
					if(bkAccInfoVO != null) {
						vo.setPayAccNm(bkAccInfoVO.getAccNm());
						vo.setPayAccNo(bkAccInfoVO.getAccNo());
					}
					
					//付款账号(html 点击弹出详情页面)
					vo.setPayAccNoHtml("<a href='#' onclick='showSysAcc("+vo.getPayAccId()+")'> <font color='blue'>"+vo.getPayAccNo()+"</font></a>");
				}
				if (vo.getRcvpymtAccId()!=null) {
					BankAccInfoDTO bankAccInfoDTO = bankAccInfoApiService.selectById(vo.getRcvpymtAccId(), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
					if(bankAccInfoDTO != null) {
						vo.setRcvpymtAccNm(bankAccInfoDTO.getAccNm());
						vo.setRcvpymtAccNo(bankAccInfoDTO.getAccNum());
					}
					
					//收款账号(html 点击弹出详情页面)
					vo.setRcvpymtAccNoHtml("<a href='#' onclick='showInsAcc("+vo.getRcvpymtAccId()+")'> <font color='blue'>"+vo.getRcvpymtAccNo()+"</font></a>");
				}
			}
		}
		return voLst;
	}

	@Override
	public Long batchUpdateRepayDetl(List<FncPrjPaySchDVO> detail) throws BaseException {
		List<FncPrjPaySchDDTO> dtoLst = FncPrjPaySchDVOTransform.toDTOList(detail);
		return fncPrjPaySchApiService.batchUpdateRepayDetl(dtoLst);
	}
	
	@Override
	public Long interestUpdateRepayDetl(List<FncPrjPaySchDVO> detail) throws BaseException {
		List<FncPrjPaySchDDTO> dtoLst = FncPrjPaySchDVOTransform.toDTOList(detail);
		return fncPrjPaySchApiService.interestUpdateRepayDetl(dtoLst);
	}

	@Override
	public Long updateRepayMainSt(FncPrjPaySchMVO entity) throws BaseException {
		FncPrjPaySchMDTO dto = FncPrjPaySchMVOTransform.toDTO(entity);
		return fncPrjPaySchApiService.updateRepayMainSt(dto);
	}

	@Override
	public boolean upateRepayMain(FncPrjPaySchMVO entity) throws BaseException {
		return fncPrjPaySchApiService.update(FncPrjPaySchMVOTransform.toDTO(entity));
	}

	@Override
	public DataResultDTO addFlowStart(FlowStartParamsDTO flowStartParamsDTO) {
		if(StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())){
			BaseLogUtils.info(logger, "addFlowStart", "用户ID和业务数据主键必须要存在!",CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return fncPrjPaySchApiService.addFlowStart(flowStartParamsDTO);
	}

	@Override
	public Long add(FncPrjPaySchMVO entity) throws BaseException {		
		return fncPrjPaySchApiService.add(FncPrjPaySchMVOTransform.toDTO(entity));
	}

	@Override
	public Long batchInsertDetail(List<FncPrjPaySchDVO> detail) throws BaseException {
		List<FncPrjPaySchDDTO> dtoLst = FncPrjPaySchDVOTransform.toDTOList(detail);
		return fncPrjPaySchApiService.batchInsertDetail(dtoLst);
	}
	
	@Override
	public List<Map<String, Object>> getRepayDetlList(long id) throws BaseException {
		return fncPrjPaySchApiService.getRepayDetlList(id);
	}

	/**
	 * 支付调息表
	 */
	@Override
	public Long updateFncPrjPayReg(FncPrjPayRegVO entity) throws BaseException {
		FncPrjPayRegDTO dto = FncPrjPayRegVOTransform.toDTO(entity);
		return fncPrjPaySchApiService.updateFncPrjPayReg(dto);
	}

	@Override
	public Map<String, Object> selectRepayProjInfoByMid(long id) throws BaseException {
		return fncPrjPaySchApiService.selectRepayProjInfoByMid(id);
	}

	@Override
	public List<FncPrjPaySchDVO> selectCostDetlLst(long id) throws BaseException {
		List<FncPrjPaySchDDTO> dtoLst = fncPrjPaySchApiService.selectRepayCostLst(id);
		List<FncPrjPaySchDVO> voLst = FncPrjPaySchDVOTransform.toVOList(dtoLst);
		if (voLst!=null) {
			for (FncPrjPaySchDVO vo:voLst) {
				if (vo.getPayAccId()!=null) {
					SysBkAccInfoVO bkAccInfoVO = sysBkAccInfoService.selectById(vo.getPayAccId());
					if(bkAccInfoVO != null) {
						vo.setPayAccNm(bkAccInfoVO.getAccNm());
						vo.setPayAccNo(bkAccInfoVO.getAccNo());
					}
					
				}
				if (vo.getRcvpymtAccId()!=null) {
					BankAccInfoDTO bankAccInfoDTO = bankAccInfoApiService.selectById(vo.getRcvpymtAccId(), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
					if(bankAccInfoDTO != null) {
						vo.setRcvpymtAccNm(bankAccInfoDTO.getAccNm());
						vo.setRcvpymtAccNo(bankAccInfoDTO.getAccNum());
					}
					
				}
			}
		}
		return voLst;
	}

	@Override
	public List<Map<String, Object>> selectCost0DetlLst(long id) throws BaseException {
		List<Map<String, Object>>  list=fncPrjPaySchApiService.selectRepayCost0Lst(id);
		if (list!=null) {
			for (Map<String, Object> vo:list) {
				if (vo.get("PAY_ACC_ID")!=null) {
					SysBkAccInfoVO bkAccInfoVO = sysBkAccInfoService.selectById(Long.valueOf(vo.get("PAY_ACC_ID").toString()));
					if(bkAccInfoVO != null) {
						vo.put("payAccNm",bkAccInfoVO.getAccNm());
						vo.put("payAccNo",bkAccInfoVO.getAccNo());
						vo.put("payAccId", bkAccInfoVO.getId());
					}
					
					//付款账号(html 点击弹出详情页面)
					vo.put("payAccNoHtml", "<a href='#' onclick='showSysAcc("+vo.get("PAY_ACC_ID")+")'> <font color='blue'>"+vo.get("payAccNo")+"</font></a>");
				}
				if (vo.get("RCVPYMT_ACC_ID")!=null) {
					BankAccInfoDTO bankAccInfoDTO = bankAccInfoApiService.selectById(Long.valueOf(vo.get("RCVPYMT_ACC_ID").toString()), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
					if(bankAccInfoDTO != null) {
						vo.put("rcvpymtAccNm",bankAccInfoDTO.getAccNm());
						vo.put("rcvpymtAccNo",bankAccInfoDTO.getAccNum());
						vo.put("rcvpymtAccId", bankAccInfoDTO.getId());
					}
					
					//收款账号(html 点击弹出详情页面)
					vo.put("rcvpymtAccNoHtml","<a href='#' onclick='showInsAcc("+vo.get("RCVPYMT_ACC_ID")+")'> <font color='blue'>"+vo.get("rcvpymtAccNo")+"</font></a>");
				}
			}
		}
		return list;
	}

	@Override
	public Long modifyFncPrjPayReg(FncPrjPayRegVO entity) throws BaseException {
		FncPrjPayRegDTO dto = FncPrjPayRegVOTransform.toDTO(entity);
		return fncPrjPaySchApiService.modifyFncPrjPayReg(dto);
	}

	@Override
	public FncPrjPaySchMVO selectById(Long id) throws BaseException {
		FncPrjPaySchMDTO dto = fncPrjPaySchApiService.selectById(id);
		FncPrjPaySchMVO vo = FncPrjPaySchMVOTransform.toVO(dto);
		return vo;
	}

	@Override
	public Integer selectExistArmt0Cnt(Long pid) throws BaseException {
		return fncPrjPaySchApiService.selectExistArmt0Cnt(pid);
	}

	@Override
	public Long insertIntoSchDJrnlFromAgo(FncPrjPaySchDJrnlVO entity) throws BaseException {
		FncPrjPaySchDJrnlDTO dto = FncPrjPaySchDJrnlVOTransform.toDTO(entity);
		return fncPrjPaySchApiService.insertIntoSchDJrnlFromAgo(dto);
	}

	@Override
	public Long insertIntoSchMJrnlFromAgo(FncPrjPaySchMJrnlVO entity) throws BaseException {
		FncPrjPaySchMJrnlDTO dto = FncPrjPaySchMJrnlVOTransform.toDTO(entity);
		return fncPrjPaySchApiService.insertIntoSchMJrnlFromAgo(dto);
	}

	@Override
	public List<Map<String, Object>> selectRepayListByCnd(FncPrjPaySchMCondition condition) {
		return fncPrjPaySchApiService.selectRepayListByCnd(condition);
	}

	@Override
	public Map<String, Object> synFundSys(Long id) throws BaseException {
		return fncPrjPaySchApiService.synFundSys(id);
	}

	/**
	 * 还款计划表查询;
	 * @return
	 */
	public Pager repaymentPlanPageList(Pager Page) throws BaseException{
		return this.fncPrjPaySchApiService.repaymentPlanPageList(Page) ;
	}
	/**
	 * 实际付款流水
	 * @return
	 */
	public Pager realPayPageList(Pager Page) throws BaseException{
		if (null == Page)
			Page = new Pager();
		Page=this.fncPrjPaySchApiService.realPayPageList(Page);
		List<Map<String, Object>>  dtoList =  (List<Map<String, Object>>) Page.getResultList();
		// 再融资-还款分解项 数据字典
		List<SysDictionaryDTO> feeClsCdList = this.sysDictionaryApiService.searchGroup(FncPrjContMainConstant.SYS_CODE, FncPrjContMainConstant.ORG_CODE, "REF0023");
		// 遍历
		ListIterator<Map<String, Object>>  iterator = dtoList.listIterator();
        while (iterator.hasNext()) {
        	Map<String, Object> map = iterator.next();
        	if(null!=map.get("CRT_USR_ID") && !"".equals(map.get("CRT_USR_ID"))){
        		SysUsrVO vo =sysUsrService.selectById(Long.parseLong(map.get("CRT_USR_ID").toString()), CurrentThreadContext.getCurrentSysCd());
				if(vo!=null){
					map.put("RL_NM", vo.getRefRlNm());
		            iterator.remove();
		            iterator.add(map);
				}
        	}
        	// 获取费用项对应的数据字典名称 by JL 20190905
        	if(null!=map.get("FEE_CLS_CD") && !"".equals(map.get("FEE_CLS_CD"))){
        		for (SysDictionaryDTO feeClsCd : feeClsCdList) {
                    if (map.get("FEE_CLS_CD").equals(feeClsCd.getCode())) {
                    	map.put("FEE_CLS_CD", feeClsCd.getCodeNm());
                        break;
                    }
                }
        	}
       }
		return Page;
	}
	/**
	 * 实际付款流水导出
	 * @return
	 */
	public String realPayExport(FncPrjContMainQueryCondition condition,String from) throws BaseException{
		return this.fncPrjPaySchApiService.realPayExport(condition,from);
	}
	/**
	 * 根据还款流水关联还款明细
	 * @return
	 */
	@Override
	public Map<String, Object> selectPayDetailByJrnl(long id) throws BaseException {
		Map<String, Object> vo = fncPrjPaySchApiService.selectPayDetailByJrnl(id);
		if (vo.get("PAY_ACC_ID")!=null) {
			SysBkAccInfoVO bkAccInfoVO = sysBkAccInfoService.selectById(Long.valueOf(vo.get("PAY_ACC_ID").toString()));
			if(bkAccInfoVO != null) {
				vo.put("payAccNm",bkAccInfoVO.getAccNm());
				vo.put("payAccNo",bkAccInfoVO.getAccNo());
				vo.put("payAccId", bkAccInfoVO.getId());
			}
			
			//付款账号(html 点击弹出详情页面)
			vo.put("payAccNoHtml", "<a href='#' onclick='showSysAcc("+vo.get("PAY_ACC_ID")+")'> <font color='blue'>"+vo.get("payAccNo")+"</font></a>");
		}
		if (vo.get("RCVPYMT_ACC_ID")!=null) {
			BankAccInfoDTO bankAccInfoDTO = bankAccInfoApiService.selectById(Long.valueOf(vo.get("RCVPYMT_ACC_ID").toString()), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
			if(bankAccInfoDTO != null) {
				vo.put("rcvpymtAccNm",bankAccInfoDTO.getAccNm());
				vo.put("rcvpymtAccNo",bankAccInfoDTO.getAccNum());
				vo.put("rcvpymtAccId", bankAccInfoDTO.getId());
			}
			
			//收款账号(html 点击弹出详情页面)
			vo.put("rcvpymtAccNoHtml","<a href='#' onclick='showInsAcc("+vo.get("RCVPYMT_ACC_ID")+")'> <font color='blue'>"+vo.get("rcvpymtAccNo")+"</font></a>");
		}
		return vo;
	}
}
